/* -------------------------------------------------------------------------
 *
 * pgstat_checkpointer.c
 *	  检查点统计的实现。
 *
 * 本文件包含检查点统计的实现。它与pgstat.c分开，以强制统计访问/存储实现与各类型统计的细节之间的界限。
 *
 * Copyright (c) 2001-2022, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
 *	  src/backend/utils/activity/pgstat_checkpointer.c
 * -------------------------------------------------------------------------
 */

#include "postgres.h"

#include "utils/pgstat_internal.h"


PgStat_CheckpointerStats PendingCheckpointerStats = {0};


/*
 * 报告检查点统计信息
 */
void pgstat_report_checkpointer(void)
{
	/* 我们假设这初始化为零 */
	static const PgStat_CheckpointerStats fc_all_zeroes;
	PgStatShared_Checkpointer *fc_stats_shmem = &pgStatLocal.shmem->checkpointer;

	Assert(!pgStatLocal.shmem->is_shutdown);
	pgstat_assert_is_up();

	/*
	 * 即使什么事情都没有发生，也可以调用此函数。在这种情况下，避免不必要地修改统计条目。
	 */
	if (memcmp(&PendingCheckpointerStats, &fc_all_zeroes,
			   sizeof(fc_all_zeroes)) == 0)
		return;

	pgstat_begin_changecount_write(&fc_stats_shmem->changecount);

#define CHECKPOINTER_ACC(fld) fc_stats_shmem->stats.fld += PendingCheckpointerStats.fld
	CHECKPOINTER_ACC(timed_checkpoints);
	CHECKPOINTER_ACC(requested_checkpoints);
	CHECKPOINTER_ACC(checkpoint_write_time);
	CHECKPOINTER_ACC(checkpoint_sync_time);
	CHECKPOINTER_ACC(buf_written_checkpoints);
	CHECKPOINTER_ACC(buf_written_backend);
	CHECKPOINTER_ACC(buf_fsync_backend);
#undef CHECKPOINTER_ACC

	pgstat_end_changecount_write(&fc_stats_shmem->changecount);

	/*
	 * 清除统计缓冲区，以便可以重复使用。
	 */
	MemSet(&PendingCheckpointerStats, 0, sizeof(PendingCheckpointerStats));
}

/*
 * pgstat_fetch_stat_checkpointer() -
 *
 * SQL可调用pgstat*函数的支持函数。返回
 * 指向检查点统计结构的指针。
 */
PgStat_CheckpointerStats *
pgstat_fetch_stat_checkpointer(void)
{
	pgstat_snapshot_fixed(PGSTAT_KIND_CHECKPOINTER);

	return &pgStatLocal.snapshot.checkpointer;
}

void pgstat_checkpointer_reset_all_cb(TimestampTz fc_ts)
{
	PgStatShared_Checkpointer *fc_stats_shmem = &pgStatLocal.shmem->checkpointer;

	/* 请参见上面的PgStatShared_Checkpointer的重置协议说明 */
	LWLockAcquire(&fc_stats_shmem->lock, LW_EXCLUSIVE);
	pgstat_copy_changecounted_stats(&fc_stats_shmem->reset_offset,
									&fc_stats_shmem->stats,
									sizeof(fc_stats_shmem->stats),
									&fc_stats_shmem->changecount);
	LWLockRelease(&fc_stats_shmem->lock);
}

void pgstat_checkpointer_snapshot_cb(void)
{
	PgStatShared_Checkpointer *fc_stats_shmem = &pgStatLocal.shmem->checkpointer;
	PgStat_CheckpointerStats *fc_reset_offset = &fc_stats_shmem->reset_offset;
	PgStat_CheckpointerStats fc_reset;

	pgstat_copy_changecounted_stats(&pgStatLocal.snapshot.checkpointer,
									&fc_stats_shmem->stats,
									sizeof(fc_stats_shmem->stats),
									&fc_stats_shmem->changecount);

	LWLockAcquire(&fc_stats_shmem->lock, LW_SHARED);
	memcpy(&fc_reset, fc_reset_offset, sizeof(fc_stats_shmem->stats));
	LWLockRelease(&fc_stats_shmem->lock);

	/* 通过重置偏移量进行补偿 */
#define CHECKPOINTER_COMP(fld) pgStatLocal.snapshot.checkpointer.fld -= fc_reset.fld;
	CHECKPOINTER_COMP(timed_checkpoints);
	CHECKPOINTER_COMP(requested_checkpoints);
	CHECKPOINTER_COMP(checkpoint_write_time);
	CHECKPOINTER_COMP(checkpoint_sync_time);
	CHECKPOINTER_COMP(buf_written_checkpoints);
	CHECKPOINTER_COMP(buf_written_backend);
	CHECKPOINTER_COMP(buf_fsync_backend);
#undef CHECKPOINTER_COMP
}
